package com.weitu.occupationalHealth.monitor.controller;

import com.alibaba.fastjson.JSONArray;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.occupationalHealth.monitor.entity.Monitor;
import com.weitu.occupationalHealth.monitor.service.MonitorService;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import com.weitu.util.TableTreeNodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/4/12.
 */
@Controller
@RequestMapping(value = "occupationalHealth/monitor")
public class MonitorController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "occupationalHealth/monitor/";

    @Autowired
    private MonitorService monitorService;

    /**
     * 跳转至监测数据页面
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public String list() {
        return MODULE_BATH_PATH + "list";
    }

    /**
     * 超标整改页面
     * @return
     */
    @RequestMapping(value = "/c/list")
    public String clist() {
        return MODULE_BATH_PATH + "cList";
    }

    @RequestMapping(value = "/a/list")
    public String alist() {
        return MODULE_BATH_PATH + "analysis";
    }

    /**
     * 新增页面
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public String add() {
        return MODULE_BATH_PATH + "add";
    }

    @ResponseBody
    @RequestMapping("insertSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——监测数据", methods = "添加监测数据")
    public AjaxResult insertSelective(@RequestBody List<Monitor> monitors) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        int boo = 0;
        User loginUser= getSessionUser();
        String loginUserId=loginUser.getId();
        String loginUserDeptId=loginUser.getDepartment().getId();
        String dateTime = DateTimeUtil.formatterDateTime();
        for (Monitor monitor:monitors){
            String uuid = IDGenerator.getId();
            monitor.setId(uuid);
            monitor.setAddUserId(loginUserId);
            monitor.setAddDeptId(loginUserDeptId);
            monitor.setAddTime(dateTime);
            monitor.setDelState(0);
        }
        paramMap.put("addList",monitors);
        boo=monitorService.insertSelective(paramMap);
        if (boo > 0) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——危害因素", methods = "删除危害因素")
    public AjaxResult deleteById(String id){
        int boo=monitorService.deleteById(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    /**
     * 跳转详情与编辑页面
     *
     * @param id
     * @return
     */
    @RequestMapping("info")
    public ModelAndView info(String id, String type) {
        ModelAndView modelAndView = new ModelAndView();
        Map map=monitorService.selectById(id);
        modelAndView.addObject("mon", map);
        if (type.equals("info")) {
            modelAndView.setViewName(MODULE_BATH_PATH + "info");
        } else {
            modelAndView.setViewName(MODULE_BATH_PATH + "edit");
        }
        return modelAndView;
    }

    /**
     * 更新危害因素
     * @return
     */
    @ResponseBody
    @RequestMapping("updateByIdSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——监测数据", methods = "更新监测数据")
    public AjaxResult updateByIdSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);
        int boo=monitorService.updateByIdSelective(paramMap);
        if (boo > 0) {
            return success("更新成功");
        } else {
            return fail("更新失败");
        }
    }

    /**
     * 加载监测数据
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/monitorTable", method = RequestMethod.POST)
    public Object monitorTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum = monitorService.selectMonitorCount(paramMap);
        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Map> dataList = monitorService.selectAllMonitor(paramMap);
        Object data = dataGridPagination.flushDataGridData(dataList);
        return data;
    }

    /**
     * 监测信息统计
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/selectAnalysis", method = RequestMethod.POST)
    public List<Map<String,Object>> selectAnalysis(HttpServletRequest request) throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        List<Map<String,Object>> maps=monitorService.selectAnalysis(paramMap);
        List<Map<String,Object>> mapList= TableTreeNodeUtil.getFatherNode(maps);
        return mapList;
    }
}
